/**
 * 
 */
package com.gzceb.zhgjj.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;

import javax.sql.DataSource;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import org.springframework.stereotype.Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserCache;

import org.springframework.dao.DataAccessException;
import org.springframework.security.core.GrantedAuthority;

import com.gzceb.zhgjj.security.dao.SysAuthoritiesResourcesDAO;
import com.gzceb.zhgjj.security.dao.SysUsersDAO;
import com.gzceb.zhgjj.security.entity.SysUsers;

/**
 * <p>
 * 该类的主要作用是为Spring Security提供一个经过用户认证后的UserDetails。
 * 该UserDetails包括用户名、密码、是否可用、是否过期等信息。
 * </p>
 * 
 * @date 2012-10-22下午6:32:36
 * @author liqing
 * @description
 */
@Service
public class MyUserDetailsService implements UserDetailsService {

	@Autowired
	private SysUsersDAO sysUsersDAO;

	@Autowired
	private SysAuthoritiesResourcesDAO sysAuthoritiesResourcesDAO;

	@Autowired
	private DataSource dataSource;

	@Autowired
	private UserCache userCache;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.security.core.userdetails.UserDetailsService#
	 * loadUserByUsername(java.lang.String)
	 */
	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException, DataAccessException {

		Collection<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();

		if (null == sysUsersDAO) {
			sysUsersDAO = new SysUsersDAO();
		}

		// 得到用户的权限
		auths = sysUsersDAO.loadUserAuthoritiesByName(username);

		// 根据用户名取得一个SysUsers对象，以获取该用户的其他信息。
		SysUsers user = sysUsersDAO.findByUserAccount(username);

		return new SysUsers(user.getUserId(), user.getUserAccount(),
				user.getUserName(), user.getUserPassword(), user.getUserDesc(),
				true, false, user.getUserDuty(), user.getUserDept(),
				user.getSubSystem(), new HashSet(0), true, true, true, auths);
	}

	// set PubUsersDao
	public void setSysUsersDAO(SysUsersDAO sysUsersDAO) {
		this.sysUsersDAO = sysUsersDAO;

	}

	public SysUsersDAO getSysUsersDAO() {
		return sysUsersDAO;
	}

	// set PubAuthoritiesResourcesHome
	public void setSysAuthoritiesResourcesDAO(
			SysAuthoritiesResourcesDAO sysAuthoritiesResourcesDAO) {
		this.sysAuthoritiesResourcesDAO = sysAuthoritiesResourcesDAO;

	}

	public SysAuthoritiesResourcesDAO getSysAuthoritiesResourcesDAO() {
		return sysAuthoritiesResourcesDAO;

	}

	// set DataSource
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public DataSource getDataSource() {
		return dataSource;
	}

	// 设置用户缓存功能。
	public void setUserCache(UserCache userCache) {
		this.userCache = userCache;
	}

	public UserCache getUserCache() {
		return this.userCache;
	}

}
